package com.marketingcenter.controller;

import com.marketingcenter.entity.MChooseNCard;
import com.marketingcenter.service.MChooseNCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * M选N权益卡控制器
 */
@RestController
@RequestMapping("/m-choose-n-cards")
public class MChooseNCardController {

    @Autowired
    private MChooseNCardService mChooseNCardService;

    /**
     * 创建M选N权益卡
     */
    @PostMapping
    public ResponseEntity<MChooseNCard> create(@RequestBody MChooseNCard card) {
        card.setType(MChooseNCard.PromotionType.M_CHOOSE_N_CARD);
        MChooseNCard createdCard = mChooseNCardService.create(card);
        return ResponseEntity.ok(createdCard);
    }

    /**
     * 更新M选N权益卡
     */
    @PutMapping("/{id}")
    public ResponseEntity<MChooseNCard> update(@PathVariable Long id, @RequestBody MChooseNCard card) {
        card.setId(id);
        MChooseNCard updatedCard = mChooseNCardService.update(card);
        return ResponseEntity.ok(updatedCard);
    }

    /**
     * 删除M选N权益卡
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        mChooseNCardService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID查询M选N权益卡
     */
    @GetMapping("/{id}")
    public ResponseEntity<MChooseNCard> getById(@PathVariable Long id) {
        MChooseNCard card = mChooseNCardService.findById(id);
        return card != null ? ResponseEntity.ok(card) : ResponseEntity.notFound().build();
    }

    /**
     * 查询所有M选N权益卡
     */
    @GetMapping
    public ResponseEntity<List<MChooseNCard>> getAll() {
        List<MChooseNCard> cards = mChooseNCardService.findAll();
        return ResponseEntity.ok(cards);
    }

    /**
     * 根据状态查询M选N权益卡
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<MChooseNCard>> getByStatus(@PathVariable Integer status) {
        List<MChooseNCard> cards = mChooseNCardService.findByStatus(status);
        return ResponseEntity.ok(cards);
    }

    /**
     * 查询活动中的M选N权益卡
     */
    @GetMapping("/active")
    public ResponseEntity<List<MChooseNCard>> getActive() {
        List<MChooseNCard> cards = mChooseNCardService.findActivePromotions();
        return ResponseEntity.ok(cards);
    }

    /**
     * 启用M选N权益卡
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Void> enable(@PathVariable Long id) {
        mChooseNCardService.enable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 停用M选N权益卡
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Void> disable(@PathVariable Long id) {
        mChooseNCardService.disable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 用户购买M选N权益卡
     */
    @PostMapping("/{id}/purchase")
    public ResponseEntity<Boolean> purchase(@PathVariable Long id, @RequestParam Long userId) {
        boolean success = mChooseNCardService.purchaseMChooseNCard(id, userId);
        return ResponseEntity.ok(success);
    }

    /**
     * 用户选择权益选项
     */
    @PostMapping("/{id}/select-options")
    public ResponseEntity<Boolean> selectOptions(@PathVariable Long id, 
                                                @RequestParam Long userId, 
                                                @RequestBody Long[] optionIds) {
        boolean success = mChooseNCardService.selectOptions(id, userId, optionIds);
        return ResponseEntity.ok(success);
    }

    /**
     * 查询用户已选择的选项
     */
    @GetMapping("/{id}/selected-options")
    public ResponseEntity<Long[]> getSelectedOptions(@PathVariable Long id, @RequestParam Long userId) {
        Long[] selectedOptions = mChooseNCardService.getUserSelectedOptions(id, userId);
        return ResponseEntity.ok(selectedOptions);
    }

}